package cn.suimg.neo4j.service.other;

import cn.suimg.neo4j.util.HttpClientUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.UUID;

/**
 * 网络剪贴板服务
 */
@Service
public class NetCutService {

    /**
     * 节点信息
     */
    @Data
    @AllArgsConstructor
    public static class Note {

        /**
         * 节点ID
         */
        private String noteId;

        /**
         * 节点名称
         */
        private String noteName;
    }

    /**
     * 访问链接
     */
    @Data
    @AllArgsConstructor
    public static class Link {

        /**
         * 只读连接
         */
        private String readOnlyLink;

        /**
         * 可编辑连接
         */
        private String editLink;
    }

    /**
     * 可编辑连接地址模板
     */
    private static final String EDIT_LINK_TEMPLATE = "https://netcut.cn/%s";

    /**
     * 只读连接地址模板
     */
    private static final String READ_ONLY_LINK_TEMPLATE = "https://netcut.cn/p/%s";

    /**
     * 上传文件政策
     */
    private static final String UPLOAD_FILE_POLICY = "eyJzYXZlLWtleSI6InVwbG9hZHMve3llYXJ9e21vbn17ZGF5fS97cmFuZG9tMzJ9IiwiZXhwaXJhdGlvbiI6MTcyMTMwMzMwNywiY29udGVudC1sZW5ndGgtcmFuZ2VcdCI6IjAsNTI0Mjg4MDAiLCJidWNrZXQiOiJuZXRjdXQtY24ifQ==";

    /**
     * 上传文件认证
     */
    private static final String UPLOAD_FILE_AUTHORIZATION = "UPYUN netcut:+n7US6H37TPG9Vn5wyXRK9KWF4I=";

    /**
     * 创建新的剪贴板
     * @param content
     * @return
     */
    public Note create(String content){
        String nodeName = uuid();
        String response = HttpClientUtil.postData("https://netcut.cn/api/note/create/", new HashMap<String, Object>() {{
            put("note_name", nodeName);
            put("note_content", content);
            put("note_pwd", "0");
            put("expire_time", "31536000");
        }});
        String nodeId = JSONObject.parseObject(response).getJSONObject("data").getString("note_id");
        return new Note(nodeId,nodeName);
    }

    /**
     * 上传文件
     * @param note
     * @param file
     */
    public void uploadFile(Note note,File file){
        //先上传文件到又拍云
        String response = HttpClientUtil.postData("https://v0.api.upyun.com/netcut-cn", new HashMap<String, Object>() {{
            put("file", file);
            put("authorization", UPLOAD_FILE_AUTHORIZATION);
            put("policy", UPLOAD_FILE_POLICY);
        }});
        JSONObject jsonObject = JSONObject.parseObject(response);

        //绑定上传文件信息到剪贴板
        HttpClientUtil.postData("https://netcut.cn/api/upload/file/",new HashMap<String, Object>(){{
            put("note_name",note.getNoteName());
            put("note_id",note.getNoteId());
            put("file_size",jsonObject.getLong("file_size"));
            put("file_name",file.getName());
            put("file_url",jsonObject.getString("url"));
        }});
    }

    /**
     * 更新剪贴板(会覆盖之前的数据)
     * @param noteId
     * @param content
     */
    public void update(String noteId,String content){
        HttpClientUtil.postJson("https://netcut.cn/api/note/update/",new HashMap<String, Object>(){{
            put("node_id",noteId);
            put("note_content",content);
        }});
    }

    /**
     * 渲染连接
     * @param node
     * @return
     */
    public Link renderLink(Note note){
        return new Link(String.format(READ_ONLY_LINK_TEMPLATE,note.getNoteId()),String.format(EDIT_LINK_TEMPLATE,note.getNoteName()));
    }

    /**
     * 生成随机ID
     * @return
     */
    private String uuid(){
        return UUID.randomUUID().toString();
    }

}
